package com.cndw.kungfu.model.general;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsDailyItem;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.dao.GeneralDao;
import com.cndw.kungfu.domain.Collect;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.PlayerOffline;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.pvp.OnlineExpAnimaWrest;
import com.cndw.kungfu.vo.EventNotice;

/**
 * 通过模块服务方法
 * 
 * 
 * @author <a href="mailto:whz-work@163.com">汪洪州</a>
 * 
 * @since 1.0 Create on 2011-8-2
 */
@Service
public class GeneralManager {

	@Resource
	private GeneralDao generalDao;

	/**
	 * 获取当天领取离线经验剩余次数
	 * 
	 * @param playerId
	 * @return
	 */
	public int getSurplusCounts(long playerId) {
		return ConstantsModel.offlineGetCounts - getOfflineExpTodayCounts(playerId, TimeProcesser.getMorningTime());
	}

	/**
	 * 获取用户今天领取的次数
	 * 
	 * @param offline
	 * @return
	 */
	public int getOfflineExpTodayCounts(long playerId, int getTime) {
		try {
			return generalDao.getOfflineExpTodayCounts(playerId, getTime);
		} catch (Exception e) {
			e.printStackTrace();
			return Integer.MAX_VALUE;
		}
	}

	/**
	 * 增加离线经验领取记录
	 * 
	 * @param offline
	 */
	public void addOfflineExp(PlayerOffline offline) {
		try {
			offline.setId(generalDao.addOfflineExp(offline).intValue());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 在线打坐
	 * 
	 * @param player
	 * @return
	 */
	public int getOnlineExp(Player player) {

		int exp = 0;

		if (player.getDataInfo().intDataGet(ConstantsIntKey.beginDaZuo) > 0) {
			synchronized (player) {
				if (player.getDataInfo().intDataGet(ConstantsIntKey.beginDaZuo) > 0) {
					int beginTime = player.getDataInfo().intDataGet(ConstantsIntKey.beginDaZuo);
					player.getDataInfo().intDataSet(ConstantsIntKey.beginDaZuo, 0);

					for (Integer level : ModeExpConfig.getOnlineExp().keySet()) {
						if (player.getLevel() >= level) {
							exp = ModeExpConfig.getOnlineExp().get(level).intValue();
							break;
						}
					}
					if (exp > 0) {
						int addRate = 0;
						// if (player.getVipLevel() > 0 &&
						// player.getVip().getMeditationR() > 0) {
						// addRate += player.getVip().getMeditationR();
						// }
						if (player.getKnight() != null) {
							addRate += player.getKnight().getMeditationR();
						}
						if (addRate > 0) {
							exp = NumberProcesser.percentClac(exp, addRate);
						}
					}

					int normalTime = 0; // 正常打坐时间
					int doubleTime = 0; // 双倍打坐时间
					int currentTime = TimeProcesser.getUnixTime(); // 当前时间
					int begin = TimeProcesser.getHourTime("14:00"); // 双倍开始时间
					int end = TimeProcesser.getHourTime("16:00"); // 双倍结束时间

					if (currentTime < begin || beginTime > end) { // 打坐在双倍时间外
						normalTime = currentTime - beginTime;
					} else {
						if (beginTime < begin) { // 开始打坐在双倍时间前
							normalTime = begin - beginTime;
							if (currentTime > end) { // 结束打坐在双倍时间后
								normalTime += currentTime - end;
								doubleTime = end - begin;
							} else { // 结束打坐在双倍时间之前
								doubleTime = currentTime - begin;
							}
						} else { // 开始打坐在双倍时间之后
							if (currentTime > end) {
								normalTime = currentTime - end;
								doubleTime = end - beginTime;
							} else {// 结束打坐在双倍时间之前
								doubleTime = currentTime - beginTime;
							}
						}
						player.getDataInfo().alterExpRet(Constants.expRet_sit);// 参加活动标记
					}

					int nomalExp = (normalTime / ConstantsModel.onlineExpInterval * exp);
					int doubleExp = (doubleTime / ConstantsModel.onlineExpInterval * exp * 2);

					// 灵气加成 - 打坐双倍经验时间内是否有灵气加成
					int animaAddRate = OnlineExpAnimaWrest.getAddRate(player.getPlayerId());
					if (animaAddRate > 0) {
						doubleExp = NumberProcesser.percentClac(doubleExp, animaAddRate);
					}

					exp = nomalExp + doubleExp;
					player.alterExp(exp);
					
					//加入到每日奖励
					player.getDataInfo().getEveryDay().finishDailyItem(ConstantsDailyItem.onlineItem);
				}
			}
		}
		return exp;
	}

	/**
	 * 离线打坐
	 * 
	 * @param player
	 * @return
	 */
	public int getOfflineExp(Player player) {
		int exp = 0;

		if (player.getDataInfo().intDataGet(ConstantsIntKey.beginDaZuo) > 0) {
			synchronized (player) {
				if (player.getDataInfo().intDataGet(ConstantsIntKey.beginDaZuo) > 0) {
					int beginTime = player.getDataInfo().intDataGet(ConstantsIntKey.beginDaZuo);
					player.getDataInfo().intDataSet(ConstantsIntKey.beginDaZuo, 0);

					for (Integer level : ModeExpConfig.getOnlineExp().keySet()) {
						if (player.getLevel() >= level) {
							exp = ModeExpConfig.getOnlineExp().get(level).intValue() / 4; // 离线打坐是在线的1/4
							break;
						}
					}

					// 最多算24小时
					int maxTime = ConstantsModel.offlineMaxHours * 60 * 60;
					int offlineTime = TimeProcesser.getUnixTime() - beginTime;
					offlineTime = offlineTime > maxTime ? maxTime : offlineTime;
					exp = offlineTime / ConstantsModel.onlineExpInterval * exp;

					player.alterExp(exp);
					player.commitSync();
					EventNotice notice = new EventNotice(ConstantsRadio.exp, ConstantsRadio.expOffline);
					notice.put("expGot", exp);
					notice.put("exp", player.getExp());
					notice.broadcast(player.getPlayerId());
				}
			}
		}
		return exp;
	}

	/**
	 * 获取所有的图鉴
	 * 
	 * @return
	 */
	public Map<Integer, Collect> getAllCollect() {
		try {
			return generalDao.getAllCollect();
		} catch (Exception e) {
			e.printStackTrace();
			return new HashMap<Integer, Collect>(1);
		}
	}

	/**
	 * 检查不可重复兑换的图鉴，玩家是否已经兑换过
	 * 
	 * @param playerId
	 * @param collectId
	 * @return
	 */
	public boolean checkCollectFinish(long playerId, int collectId) {
		try {
			return generalDao.checkCollectFinish(playerId, collectId) > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
	}

	/**
	 * 添加用户兑换图鉴记录
	 * 
	 * @param playerId
	 * @param collectId
	 */
	public void addPlayerCollect(long playerId, int collectId) {
		try {
			generalDao.addPlayerCollect(playerId, collectId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 递增用户兑换图鉴次数
	 * 
	 * @param playerId
	 * @param collectId
	 */
	public void increasePlayerCollect(long playerId, int collectId) {
		try {
			generalDao.increasePlayerCollect(playerId, collectId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private int getPrayGold(int times) {
		if (times <= 10) {
			return ConstantsModel.prayGold * times;
		} else if (times <= 30) {
			return 20;
		} else if (times <= 35) {
			return 30;
		} else if (times <= 40) {
			return 40;
		} else if (times <= 50) {
			return 50;
		} else if (times <= 60) {
			return 60;
		} else if (times <= 70) {
			return 80;
		} else {
			return 100;
		}
	}

	public int getPrayGoldBath(int haveTimes, int count) {
		int costGold = 0;
		for (int i = 1; i <= count; i++) {
			costGold += getPrayGold(haveTimes + i);
		}
		return costGold;
	}
}
